package com.irun.pm.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.ui.FugitivesQueryActivity.FugitiveAdapter;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-25
 * @email huangsanm@gmail.com
 * @desc population_query
 */
public class PopulationQueryActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private static final int REQUEST_ADD = 1;
	private List<DriverInfo> listFugitives = new ArrayList<DriverInfo>();
	private FugitiveAdapter fugitiveAdapter;
	private ListView lvPopulation;
	private EditText etName, etCard;
	private DriverInfoControllers controller;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.population_query);
		findViewById(R.id.btnFugitiveQuery).setOnClickListener(this);
		findViewById(R.id.btnAdd).setOnClickListener(this);
		lvPopulation = (ListView) findViewById(R.id.lvPopulation);
		etName = (EditText) findViewById(R.id.etName);
		etCard = (EditText) findViewById(R.id.etCard);
		lvPopulation.setOnItemClickListener(this);
		controller = new DriverInfoControllers(this);
		listFugitives = controller.getDriverInfoList(Utils.TYPE_PEOPLE_QUERY);
		fugitiveAdapter = new FugitiveAdapter(listFugitives);
		lvPopulation.setAdapter(fugitiveAdapter);
		
	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnFugitiveQuery:
			doProgress("提示", "正在搜索中...", new Callable<String>() {
				@Override
				public String call() throws Exception {
					// TODO Auto-generated method stub
					Thread.sleep(2000);
					return null;
				}
			}, new Callback<String>() {
				@Override
				public void onCallback(String pCallbackValue) {
					// TODO Auto-generated method stub
					String name = etName.getText().toString().trim();
					String card = etCard.getText().toString().trim();
					if (StringUtil.isEmpty(name) && StringUtil.isEmpty(card)) {
						listFugitives = controller.getDriverInfoList(Utils.TYPE_PEOPLE_QUERY);
						fugitiveAdapter = new FugitiveAdapter(listFugitives);
						lvPopulation.setAdapter(fugitiveAdapter);
						Toast.makeText(PopulationQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
						return;
					}

					listFugitives.clear();
					if (!StringUtil.isEmpty(name) && !StringUtil.isEmpty(card)) {
						// 两个条件一起搜索
						listFugitives = controller.queryDriverInfoByName(name, Utils.TYPE_PEOPLE_QUERY);
					} else if (!StringUtil.isEmpty(card)) {
						// 单个条件搜索身份证
						DriverInfo driverInfo = controller.queryDriverInfoByCard(card, Utils.TYPE_PEOPLE_QUERY);
						if(driverInfo!=null)
							listFugitives.add(driverInfo);
					} else if (!StringUtil.isEmpty(name)) {
						// 单个条件搜索姓名
						listFugitives = controller.queryDriverInfoByName(name, Utils.TYPE_PEOPLE_QUERY);
					}
					if(!listFugitives.isEmpty()){
						Toast.makeText(PopulationQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(PopulationQueryActivity.this, "查询不到任何数据", Toast.LENGTH_SHORT).show();
					}
					fugitiveAdapter = new FugitiveAdapter(listFugitives);
					lvPopulation.setAdapter(fugitiveAdapter);
				}
			}, null);
			
			break;
		case R.id.btnAdd:
			Intent intent = new Intent(this,PopulationAddActivity.class);
			startActivityForResult(intent, REQUEST_ADD);
			break;
		default:
			break;
		}
	}

	class FugitiveAdapter extends BaseAdapter {
		private List<DriverInfo> listFugitives;

		public FugitiveAdapter(List<DriverInfo> listFugitives) {
			this.listFugitives = listFugitives;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listFugitives.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listFugitives.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(PopulationQueryActivity.this)
						.inflate(R.layout.car_item, null);
				holder = new ViewHolder();
				holder.tvCardNum = (TextView) convertView
						.findViewById(R.id.tvCardNum);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				holder.tvName = (TextView) convertView
						.findViewById(R.id.tvName);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvCardNum.setText(listFugitives.get(position).getName());
			holder.tvTime.setText(listFugitives.get(position).getSex());
			holder.tvName.setText(listFugitives.get(position).getCard());
			return convertView;
		}

	}

	class ViewHolder {
		TextView tvCardNum;
		TextView tvTime;
		TextView tvName;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		DriverInfo driverInfo = (DriverInfo)parent.getItemAtPosition(position);
		Intent intent = new Intent(this, PopulationDetailsActivity.class);
		intent.putExtra("id", driverInfo.getId());
		startActivity(intent);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(resultCode == RESULT_CANCELED){
			return ;
		}
		switch (requestCode) {
		case REQUEST_ADD:
			listFugitives = controller.getDriverInfoList(Utils.TYPE_PEOPLE_QUERY);
			fugitiveAdapter = new FugitiveAdapter(listFugitives);
			lvPopulation.setAdapter(fugitiveAdapter);
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
